﻿using FluorineFx.Json;
using lxwmvc;
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

namespace lxwmvc
{
    public class WeiXin_Map
    {
        /// <summary>
        /// 通过IP地址得到城市
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public static string GetCity(string ip)
        {
            try
            {
                string json = WeiXin_Fun.GetUrl("http://ip.taobao.com/service/getIpInfo.php?ip=" + ip, false, "utf-8");
                json = json.Replace("\\", "%");

                if (json != "")
                {
                    json = HttpUtility.UrlDecode(json);
                    JavaScriptObject tbIP = JavaScriptConvert.DeserializeObject(json) as JavaScriptObject;
                    JavaScriptObject data = tbIP["data"] as JavaScriptObject;
                    return data != null && data.ContainsKey("city") ? data["city"].tostr() : "";
                }
            }
            catch (Exception err)
            {
                dataloger.fatal(err.Message, err);
            }

            return WeiXin_Fun.GetUrl("http://whois.pconline.com.cn/ip.jsp?ip=" + ip, false, "gbk");
        }

        /// <summary>
        /// 根据手机号 城市 地址很短
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        public static string GetPhoneCity(string phone)
        {
            string city = "";
            try
            {
                //淘宝方式 好像经常获取不到？？？
                string jsonStr = WeiXin_Fun.GetUrl("http://virtual.paipai.com/extinfo/GetMobileProductInfo?mobile=" + phone + "&amount=10000&callname=getPhoneNumInfoExtCallback", false, "GBK");
                city = WeiXin_Fun.SubString(jsonStr, "cityname:'", "'})");

                if (city.Length > 0)
                {
                    return city;
                }

                //财付通方式
                string xmlStr = WeiXin_Fun.GetUrl("http://life.tenpay.com/cgi-bin/mobile/MobileQueryAttribution.cgi?chgmobile=" + phone, false, "gbk");
                city = WeiXin_Fun.SubString(xmlStr, "<city>", "</city>");

                if (city.Length > 1)
                {
                    return city;
                }
            }
            catch (Exception ex)
            {
                dataloger.fatal("", ex);
            }

            return city;
        }


        /// <summary>
        /// 根据GPS 返回城市(通过百度转换一下 把gps)
        /// </summary>
        /// <param name="x">GPS X</param>
        /// <param name="y">GPS Y</param>
        /// <param name="loc">百度经度y lng 百度纬度x lat</param>
        /// <param name="info"></param>
        /// <returns>城市 地级市</returns>
        public static string GetGPSCity(string x, string y, out JavaScriptObject loc, out JavaScriptObject info)
        {
            string city = "";
            string json = "";
            try
            {

                json = WeiXin_Fun.GetUrl("http://api.map.baidu.com/geocoder/v2/?ak=yhYPBbk4UkzyeTk0tfpFuKtY&coordtype=wgs84ll&location=" + y + "," + x + "&output=json");
                JavaScriptObject adderss = JavaScriptConvert.DeserializeObject(json) as JavaScriptObject;

                adderss = adderss["result"] as JavaScriptObject;
                loc = adderss["location"] as JavaScriptObject;

                //{"status":0,"result":{"location":{"lng":100.00734277893,"lat":12.004461500047},"formatted_address":"","business":"","addressComponent":{"city":"","country":"","direction":"","distance":"","district":"","province":"","street":"","street_number":"","country_code":0},"poiRegions":[],"sematic_description":"","cityCode":0}}


                //data["y"] = location.v_s("lng");//百度经度y lng
                //data["x"] = location.v_s("lat");//百度纬度x lat
                info = adderss["addressComponent"] as JavaScriptObject;

                //string address = addressComponent.v_s("province") + " " + addressComponent.v_s("city") + " " + addressComponent.v_s("district") + " " + adderss.v_s("sematic_description");//省市区 地址
                info["sematic_description"] = WeiXin_Fun.Get<string>(adderss, "sematic_description") ?? "";
                city = WeiXin_Fun.Get<string>(info, "city") ?? "";
            }
            catch (Exception ex)
            {
                dataloger.fatal("x:" + x + " y:" + y + " 地址" + json, ex);
                loc = new JavaScriptObject();
                info = new JavaScriptObject();
            }
            return city;
        }


        #region 百度坐标计算 cc 2014.08.11

        /// <summary>
        /// 地球半径（百度）
        /// </summary>
        static double EARTHRADIUS = 6370996.81;

        /// <summary>
        /// 将度转化为弧度
        /// </summary>
        /// <param name="degree">度</param>
        /// <returns>弧度</returns>
        static double DegreeToRad(double degree)
        {
            return Math.PI * degree / 180;
        }

        /// <summary>
        /// 计算两点之间的距离,两点坐标必须为经纬度
        /// </summary>
        /// <param name="x_1"></param>
        /// <param name="y_1"></param>
        /// <param name="x_2"></param>
        /// <param name="y_2"></param>
        /// <returns></returns>
        public static double GetDistance(double x_1, double y_1, double x_2, double y_2)
        {
            double x1, x2, y1, y2;
            x1 = DegreeToRad(x_1);
            y1 = DegreeToRad(y_1);
            x2 = DegreeToRad(x_2);
            y2 = DegreeToRad(y_2);

            

            return EARTHRADIUS * Math.Acos((Math.Sin(y1) * Math.Sin(y2) + Math.Cos(y1) * Math.Cos(y2) * Math.Cos(x2 - x1)));
            //return GetShortDistance(point1.X, point1.Y, point2.X, point2.Y);
        }

        #endregion
    }
}
